package cn.zwxky.android.staff.HomePage;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.zgscwjm.lsfbbasetemplate.utils.LsfbLog;

/**
 * Created by Administrator on 2017/3/24 0024.
 */

public class LocationDBHelper extends SQLiteOpenHelper {

    public static final int VERSION = 1;
    public static final String DATABASE_NAME = "location_db_staff";
    private final static String TABLE_NAME = "location_staff";
    public final static String LOCATION_ID = "location_id";
    public final static String LONGITUDE = "longitude";
    public final static String LATITUDE = "latitude";
    public final static String ELEVATION = "elevation";
    public final static String TIME = "time";

    public LocationDBHelper(Context context) {
        super(context, DATABASE_NAME, null, VERSION);
    }

    // 当第一次创建数据库的时候，调用该方法
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE " + TABLE_NAME + " (" + LOCATION_ID + " INTEGER primary key autoincrement, " + LONGITUDE + " text, " + LATITUDE + " text, "+ ELEVATION + " text, " + TIME + " text);";
        //输出创建数据库的日志信息
        LsfbLog.e("create Database------------->");
        //execSQL函数用于执行SQL语句
        db.execSQL(sql);
    }

    //当更新数据库的时候执行该方法
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //输出更新数据库的日志信息
        String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
        db.execSQL(sql);
        onCreate(db);
        LsfbLog.e("update Database------------->");
    }

    //增加操作
    public long insert(String longitude, String latitude, String elevation,String time) {
        SQLiteDatabase db = this.getWritableDatabase();
/* ContentValues */
        ContentValues cv = new ContentValues();
        cv.put(LONGITUDE, longitude);
        cv.put(LATITUDE, latitude);
        cv.put(ELEVATION, elevation);
        cv.put(TIME, time);
        long row = db.insert(TABLE_NAME, null, cv);
        return row;
    }

    //删除操作
    public void delete(int id) {
//        SQLiteDatabase db = this.getWritableDatabase();
//        String where = LOCATION_ID + " = " + String.valueOf(id);
////        String[] whereValue = {};
//        try {
//            db.delete(TABLE_NAME, where, null);
//        } catch (Exception e) {
//            LsfbLog.e(e);
//        }

        SQLiteDatabase db = this.getWritableDatabase();
        String where = LOCATION_ID + " = ?";
        String[] whereValue = {Integer.toString(id)};
        db.delete(TABLE_NAME, where, whereValue);

    }

    public Cursor select() {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db
                .query(TABLE_NAME, null, null, null, null, null, null);
        return cursor;
    }
}
